/**
 * 
 */
package site.service.weibo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * @author JinJing
 *
 */
public class HttpPage {
	private final static int CONNECTION_TIMEOUT = 30000;
	private final static int SOCKET_TIMEOUT = 30000;
	/*
	 * 根据一个url用get方法获取相应的页面
	 */
	public static String getPageByHttpClient(String url) {
		String page = null;
		HttpClient client = new HttpClient();
		client.getParams().setIntParameter("http.connection.timeout",
				CONNECTION_TIMEOUT);
		client.getParams().setIntParameter("http.socket.timeout",
				SOCKET_TIMEOUT);
		try {
			//System.out.println(url);
			HttpMethod method = new GetMethod(url);
			InputStream input;
			BufferedReader reader;
			String line;
			StringBuffer buffer = new StringBuffer();
			client.executeMethod(method);
			input = method.getResponseBodyAsStream();
			reader = new BufferedReader(new InputStreamReader(input, "utf-8"));
			while (reader!=null&&(line = reader.readLine()) != null) {
				buffer.append(line + "\r\n");
			}
			page = buffer.toString();
			
			reader.close();
			input.close();
			method.releaseConnection();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return page;
	}
}
